package com.wqtz.chechai.view.activities;

import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.util.TypedValue;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.Toast;

import com.wqtz.chechai.R;
import com.wqtz.chechai.adapter.ApprovalInfoAdapter;
import com.wqtz.chechai.base.BaseActivity;
import com.wqtz.chechai.bean.ApprovalInfoBean;
import com.wqtz.chechai.presenter.BasePresent;
import com.wqtz.chechai.utils.SPUtils;
import com.wqtz.chechai.view.fragments.MessageFragment;
import com.yinglan.swiperefresh.PullToRefreshBase;
import com.yinglan.swiperefresh.PullToRefreshSwipeListView;
import com.yinglan.swiperefresh.SwipeMenu;
import com.yinglan.swiperefresh.SwipeMenuCreator;
import com.yinglan.swiperefresh.SwipeMenuItem;
import com.yinglan.swiperefresh.SwipeMenuListView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目名称： CheChai
 * 创建人 :  Kisum
 * 创建时间: 2017/2/17
 * 审批消息列表
 */
public class ApprovalInfoActivity extends BaseActivity {
    /**
     * 需要展示的ApprovalInfoBean数据
     */
    private List<ApprovalInfoBean> mDatas;
    private PullToRefreshSwipeListView mPullRefreshListView;
    int requsetDataNum = 0;
    private ApprovalInfoAdapter mAdapter;
    private ImageView ivBack;

    @Override
    public void init() {

        mPullRefreshListView = (PullToRefreshSwipeListView) findViewById(R.id.pull_to_refresh_list);
        ivBack = (ImageView) findViewById(R.id.iv_back);
        mDatas = new ArrayList<>();
        mAdapter = new ApprovalInfoAdapter(this, R.layout.item_approval_detail);
        triggerLoadData(10, true);
        mPullRefreshListView.setAdapter(mAdapter);
        mPullRefreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //跳转到审批详情
                Intent intent = new Intent(ApprovalInfoActivity.this, ApprovalInfoDetailActivity.class);
                intent.putExtra("type", position);
                startActivity(intent);
            }
        });
        mPullRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<SwipeMenuListView>() {
            //下拉刷新
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<SwipeMenuListView> refreshView) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(ApprovalInfoActivity.this, "下拉刷新", Toast.LENGTH_SHORT).show();
                        //触发加载数据
                        triggerLoadData(10, true);

                    }
                }, 2000);
            }

            //上拉加载
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<SwipeMenuListView> refreshView) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(ApprovalInfoActivity.this, "加载更多", Toast.LENGTH_SHORT).show();
                        //触发加载数据
                        triggerLoadData(10, false);

                    }
                }, 2000);
            }
        });
        mPullRefreshListView.getRefreshableView().setOnMenuItemClickListener(mAdapter);
        //创建侧滑菜单
        mPullRefreshListView.getRefreshableView().setMenuCreator(creator);
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent toMessageFragmentIntent = new Intent(ApprovalInfoActivity.this,MainActivity.class);
                toMessageFragmentIntent.putExtra("id",1);
                startActivity(toMessageFragmentIntent);
            }
        });
    }

    @Override
    public void loadData() {

    }

    @Override
    public int setContentViews() {
        return R.layout.activity_approval_info;
    }

    @Override
    public BasePresent createPresenter() {
        return null;
    }

    private void triggerLoadData(int data, boolean isResfresh) {
        mDatas.clear();
        if (isResfresh) {
            requsetDataNum = 2;
        } else {
            requsetDataNum += data;
        }
        //模拟加载数据
        for (int i = 0; i < requsetDataNum; i++) {
            ApprovalInfoBean approvalInfoBean = new ApprovalInfoBean();
            approvalInfoBean.setApprovalStatus(i % 2);//两种类型
            approvalInfoBean.setApprovalName("张婷");
            approvalInfoBean.setApprovalInfo("[广州本田 雅阁 EX2010款...]报价为8万元");
            approvalInfoBean.setApprovalTime("10:27");
            mDatas.add(approvalInfoBean);
        }
//        loadDatas();
        mAdapter.clearData();
        mAdapter.addData(mDatas);
        mPullRefreshListView.onRefreshComplete();
    }

    private SwipeMenuCreator creator = new SwipeMenuCreator() {


        @Override
        public void create(SwipeMenu menu) {
            createMenu1(menu);
        }

        /**
         * 创建查看，删除菜单
         * @param menu
         */
        private void createMenu1(SwipeMenu menu) {
            SwipeMenuItem item1 = new SwipeMenuItem(
                    ApprovalInfoActivity.this);
            item1.setBackground(R.mipmap.menu_del);
            item1.setWidth(dp2px(90));
            item1.setTitleColor(Color.WHITE);
            item1.setTitleSize(18);
            item1.setTitle(R.string.delete);
            menu.addMenuItem(item1);
            SwipeMenuItem item2 = new SwipeMenuItem(
                    ApprovalInfoActivity.this);

            item2.setWidth(dp2px(90));
            item2.setBackground(R.mipmap.menu_see);
            item2.setTitleColor(Color.WHITE);
            item2.setTitleSize(18);
            item2.setTitle(R.string.look);
            menu.addMenuItem(item2);
        }


        private int dp2px(int dp) {
            return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
                    getResources().getDisplayMetrics());
        }
    };

    //跳转到审批详情
    private void toApprovalInfoActivity(int position) {
        //
        Intent intent = new Intent(this, ApprovalInfoDetailActivity.class);
        ApprovalInfoBean approvalInfoBean = mDatas.get(position);
        intent.putExtra("type", approvalInfoBean.getApprovalStatus());
        startActivity(intent);
    }
}
